﻿using Microsoft.EntityFrameworkCore;
using Sowrk03_28.Repository;
using Sowrk03_28.Repository.Impls;
using Sowrk03_28.Repository.Interface;
using RBAC.Repository;
using RBAC.Server;
using RBAC.Server.Interface;
using RBAC.Server.Impls;
using RBAC.Domain;
using Swork03_28.Services.Interface;
using Swork03_28.Services.Impls;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using System.Security.AccessControl;
using Architecture.Repository.Interface;
using Architecture.Repository.Impls;
using Architercture.Domain.Service.IService;
using Architercture.Domain.Service.Impls;

namespace Swork03_28.Api.Extensions
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddServices(this IServiceCollection services, string conn)
        {
            services.AddDbContext<MyDbContext>(x => x.UseSqlServer(conn));


            services.AddTransient<IUserRepository, UserRepository>();
            services.AddTransient<IUserServer, UserServer>();


            services.AddTransient<IRoleRepository, RoleRepository>();
            services.AddTransient<IRoleServer, MyRoleServer>();


            services.AddTransient<IMenuRepository, MenuRepository>();
            services.AddTransient<IMenuServer, MenuServer>();


            services.AddTransient<ITraceablesRepository, TraceableRepository>();
            services.AddTransient<ITraceableService, TraceableService>();


            services.AddTransient<IGoodsRepository, GoodsRepository>();
            services.AddTransient<IGoodsService, GoodsService>();

            services.AddTransient<IFarmedRepository, FarmedRepository>();
            services.AddTransient<IFarmedService, FarmedService>();

            services.AddTransient<IDeepProcessRepository, DeepProcessRepository>();
            services.AddTransient<IDeepProcessService, DeepProcessService>();

            services.AddTransient<ISupplierRepository, SupplierRepository>();
            services.AddTransient<ISupplierService, SupplierService>();

            return services;
        }

        public static IServiceCollection AddRepository(this IServiceCollection services)
        {
            services.AddTransient(typeof(IRepository<>), typeof(Repository<>));

            return services;
        }

        /// <summary>
        /// 注册JWT
        /// </summary>
        /// <param name="services"></param>
        /// <param name="builder"></param>
        public static void AddJWT(this IServiceCollection services, WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                //取出密钥
                var secretByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    //验证发布者
                    ValidateIssuer = true,
                    ValidIssuer = builder.Configuration["Authentication:Issuer"],
                    //验证接收者
                    ValidateAudience = true,
                    ValidAudience = builder.Configuration["Authentication:Audience"],
                    //验证是否过期
                    ValidateLifetime = true,
                    //验证私钥
                    IssuerSigningKey = new SymmetricSecurityKey(secretByte)
                };
            });
        }
    }
}
